﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.FrostLegion
{
    public class SnowmanTank : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 3;
        }
        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            var DropRules = Main.ItemDropsDB.GetRulesForNPCID(NPCID.SnowBalla, false);
            foreach (var DropRule in DropRules)
            {
                npcLoot.Add(DropRule);
            }
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (!NPC.AnyNPCs(144) || !Main.hardMode || spawnInfo.SpawnTileY >= Main.worldSurface)
            {
                return 0f;
            }
            return 0.02f;
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Invasions.FrostLegion,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.NPCs.SnowmanTank.Bestiary")
            ]);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int num788 = 0; num788 < 7; num788++)
                {
                    int num789 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Smoke, 0f, 0f, 100, default, 1.5f);
                    Dust dust = Main.dust[num789];
                    dust.velocity *= 1.4f;
                }
                for (int num790 = 0; num790 < 4; num790++)
                {
                    int num791 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Torch, 0f, 0f, 100, default, 2.5f);
                    Main.dust[num791].noGravity = true;
                    Dust dust = Main.dust[num791];
                    dust.velocity *= 5f;
                    num791 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Torch, 0f, 0f, 100, default, 1.5f);
                    dust = Main.dust[num791];
                    dust.velocity *= 3f;
                }
                int num792 = Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X, NPC.position.Y), default, Main.rand.Next(61, 64));
                Gore gore2 = Main.gore[num792];
                gore2.velocity *= 0.4f;
                Main.gore[num792].velocity.X += 1f;
                Main.gore[num792].velocity.Y += 1f;
                num792 = Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X, NPC.position.Y), default, Main.rand.Next(61, 64));
                gore2 = Main.gore[num792];
                gore2.velocity *= 0.4f;
                Main.gore[num792].velocity.X -= 1f;
                Main.gore[num792].velocity.Y -= 1f;
            }
        }
        public override void SetDefaults()
        {
            NPC.width = 70;
            NPC.height = 140;
            NPC.aiStyle = -1;
            NPC.damage = 100;
            NPC.defense = 56;
            NPC.lifeMax = 1800;
            NPC.HitSound = SoundID.NPCHit4;
            NPC.DeathSound = SoundID.NPCDeath14;
            NPC.knockBackResist = 0f;
            NPC.value = 10000f;
            NPC.npcSlots = 4f;
            if (!Main.dedServ)
            {
                Music = MusicID.Boss3;
            }
        }
        public override void FindFrame(int frameHeight)
        {
            NPC.spriteDirection = NPC.direction;

            if (Math.Abs(NPC.velocity.X) > 1f)
            {
                NPC.frameCounter += 1;
                if (NPC.frameCounter >= 8)
                {
                    NPC.frameCounter = 0;
                    NPC.frame.Y += frameHeight;
                    if (NPC.frame.Y >= frameHeight * Main.npcFrameCount[NPC.type])
                    {
                        NPC.frame.Y = 0;
                    }
                }
            }
            else
            {
                NPC.frame.Y = 0;
                NPC.frameCounter = 0;
            }
        }
        public override void AI()
        {
            float num966 = 2f;
            NPC.noGravity = true;
            NPC.noTileCollide = true;
            NPC.TargetClosest();
            bool flag47 = false;
            if (NPC.life < NPC.lifeMax * 0.75)
            {
                num966 = 3f;
            }
            if (NPC.life < NPC.lifeMax * 0.5)
            {
                num966 = 4f;
            }
            if (NPC.life < NPC.lifeMax * 0.25)
            {
                num966 = 5f;
            }
            Vector2 center4 = NPC.Center;
            Point point5 = center4.ToTileCoordinates();
            if (WorldGen.InWorld(point5.X, point5.Y) && !WorldGen.SolidTile(point5.X, point5.Y))
            {
                Lighting.AddLight(center4, 0.3f, 0.26f, 0.05f);
            }
            else if (NPC.ai[0] == 0f)
            {
                NPC.ai[1] += 1f;
                if (NPC.ai[1] >= 300f && Main.netMode != NetmodeID.MultiplayerClient)
                {
                    NPC.TargetClosest();
                    NPC.ai[1] = 0f;
                    NPC.ai[0] = 1f;
                    NPC.netUpdate = true;
                }
            }

            NPC.ai[2] += 2.0f;
            if (NPC.ai[2] >= 10f)
            {
                NPC.ai[2] = 0f;
                if (Main.netMode != NetmodeID.MultiplayerClient)
                {
                    Vector2 vector70 = new(
                        NPC.position.X + NPC.width * 0.5f - NPC.direction * 12,
                        NPC.position.Y + NPC.height * 0.85f
                    );

                    float angleOffset = MathHelper.ToRadians(Main.rand.Next(-5, 5));
                    float baseSpeed = 25f;
                    float speedX = baseSpeed * NPC.spriteDirection * (float)Math.Cos(angleOffset);
                    float speedY = baseSpeed * (float)Math.Sin(angleOffset);

                    if (Main.netMode != NetmodeID.MultiplayerClient)
                    {
                        int num575 = 25;
                        int num576 = 110;
                        int num577 = Projectile.NewProjectile(NPC.GetSource_FromAI(), vector70.X, vector70.Y, speedX, speedY, num576, num575, 0f, Main.myPlayer);
                        Main.projectile[num577].ai[0] = 2f;
                        Main.projectile[num577].timeLeft = 300;
                        Main.projectile[num577].friendly = false;
                        Main.projectile[num577].velocity *= 0.9f + Main.rand.NextFloat() * 0.2f;

                        NetMessage.SendData(MessageID.SyncProjectile, -1, -1, null, num577);
                        NPC.netUpdate = true;
                    }
                }
            }
            if (NPC.ai[1] > 240f)
            {
                NPC.ai[0] = 0f;
                NPC.ai[1] = 0f;
            }
            if (Main.netMode != NetmodeID.MultiplayerClient)
            {
                int num974 = 1200;
                int num975 = 2700;
                if (NPC.life < NPC.lifeMax * 0.25)
                {
                    num974 = (int)(num974 * 0.5);
                    num975 = (int)(num975 * 0.5);
                }
                else if (NPC.life < NPC.lifeMax * 0.5)
                {
                    num974 = (int)(num974 * 0.75);
                    num975 = (int)(num975 * 0.75);
                }
                else if (NPC.life < NPC.lifeMax * 0.75)
                {
                    num974 = (int)(num974 * 0.9);
                    num975 = (int)(num975 * 0.9);
                }
                if (Main.rand.NextBool(num974))
                {
                    NPC.localAI[1] = 1f;
                }
                if (NPC.localAI[1] >= 1f)
                {
                    NPC.localAI[1] += 1f;
                    if (NPC.localAI[1] >= 100f)
                    {
                        NPC.localAI[1] = 0f;
                    }
                }
                if (Main.rand.NextBool(num975))
                {
                    NPC.localAI[2] = 2f;
                }
                if (NPC.localAI[2] > 0f)
                {
                    NPC.localAI[2] += 1f;
                    if (NPC.localAI[2] >= 100f)
                    {
                        NPC.localAI[2] = 0f;
                    }
                }
            }
            if (Math.Abs(NPC.Center.X - Main.player[NPC.target].Center.X) < 50f)
            {
                flag47 = true;
            }
            if (flag47)
            {
                NPC.velocity.X *= 0.9f;
                if (NPC.velocity.X > -0.1 && NPC.velocity.X < 0.1)
                {
                    NPC.velocity.X = 0f;
                }
            }
            else
            {
                if (NPC.direction > 0)
                {
                    NPC.velocity.X = (NPC.velocity.X * 20f + num966) / 21f;
                }
                if (NPC.direction < 0)
                {
                    NPC.velocity.X = (NPC.velocity.X * 20f - num966) / 21f;
                }
            }
            int num993 = 80;
            int num994 = 20;
            Vector2 vector131 = new(NPC.Center.X - num993 / 2, NPC.position.Y + NPC.height - num994);
            bool flag48 = false;
            if (NPC.position.X < Main.player[NPC.target].position.X && NPC.position.X + NPC.width > Main.player[NPC.target].position.X + Main.player[NPC.target].width && NPC.position.Y + NPC.height < Main.player[NPC.target].position.Y + Main.player[NPC.target].height - 16f)
            {
                flag48 = true;
            }
            if (flag48)
            {
                NPC.velocity.Y += 0.5f;
            }
            else if (Collision.SolidCollision(vector131, num993, num994))
            {
                if (NPC.velocity.Y > 0f)
                {
                    NPC.velocity.Y = 0f;
                }
                if (NPC.velocity.Y > -0.2)
                {
                    NPC.velocity.Y -= 0.025f;
                }
                else
                {
                    NPC.velocity.Y -= 0.2f;
                }
                if (NPC.velocity.Y < -4f)
                {
                    NPC.velocity.Y = -4f;
                }
            }
            else
            {
                if (NPC.velocity.Y < 0f)
                {
                    NPC.velocity.Y = 0f;
                }
                if (NPC.velocity.Y < 0.1)
                {
                    NPC.velocity.Y += 0.025f;
                }
                else
                {
                    NPC.velocity.Y += 0.5f;
                }
            }
            if (NPC.velocity.Y > 10f)
            {
                NPC.velocity.Y = 10f;
            }
        }
    }
}
